/**
 * 
 */
package optimization.naturalOptimization.terminationCriterion;

import optimization.naturalOptimization.NaturalOptimization;

/**
 * This class provides a termination criterion in dependence on calculation
 * time.
 * 
 * @author Kevin Wagner
 * @version 1.0
 */
public class CalculationTimeTermination extends TerminationCriterion {

	private long startTime = 0;
	private long limit;

	/**
	 * Creates a new Termination Criterion with a given parent optimization.
	 * 
	 * @param opt
	 *            parent optimization
	 */
	public CalculationTimeTermination(NaturalOptimization opt) {
		super(opt);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * optimization.naturalOptimization.terminationCriterion.TerminationCriterion
	 * #isFinished()
	 */
	@Override
	public boolean isFinished() {
		if (startTime == 0) {
			startTime = System.currentTimeMillis();
			limit = getParentOptimization().getCalculationTime();
			return false;
		}
		long difference = System.currentTimeMillis() - startTime;
		if (difference >= limit) {
			return true;
		}
		return false;
	}

	@Override
	public String getIndentification() {
		long timeMillis = getParentOptimization().getCalculationTime();
		long time = timeMillis / 1000;
		String seconds = Integer.toString((int) (time % 60));
		String minutes = Integer.toString((int) ((time % 3600) / 60));
		String hours = Integer.toString((int) (time / 3600));
		for (int i = 0; i < 2; i++) {
			if (seconds.length() < 2) {
				seconds = "0" + seconds;
			}
			if (minutes.length() < 2) {
				minutes = "0" + minutes;
			}
			if (hours.length() < 2) {
				hours = "0" + hours;
			}
		}
		return "Calculation Time Termination Criterion\n  " + hours + ":"
				+ minutes + ":" + seconds + " (hh:mm:ss)";
	}

}
